package com.pmenergy.control;

// Generated 15/08/2014 01:13:31 AM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




import com.pmenergy.model.Cliente;
import com.pmenergy.model.Item;

/**
 * Home object for domain model class Item.
 * @see com.pmenergy.model.Item
 * @author Hibernate Tools
 */
@Stateless
public class ItemHome {

	private static final Log log = LogFactory.getLog(ItemHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public int persist(Item transientInstance) {
		int result=1;
		log.debug("persisting Item instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
			return result;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			return -1;
		}
	}

	public void remove(Item persistentInstance) {
		log.debug("removing Item instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Item merge(Item detachedInstance) {
		log.debug("merging Item instance");
		try {
			Item result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Item findById(BigDecimal id) {
		log.debug("getting Item instance with id: " + id);
		try {
			Item instance = entityManager.find(Item.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
		
	public int removeById(BigDecimal id) {
		log.debug("removing Item instance with id: " + id);
		try {
			int deletedItems = entityManager.createQuery("DELETE FROM Item c WHERE c.itemId = :id")
					.setParameter("id",id).executeUpdate();
			log.debug("get successful");
			return deletedItems;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<Item> findAllItems() {
		log.debug("getting all Items instances " );
		try {
			List<Item> list = entityManager.createQuery("SELECT c FROM Item c ").getResultList();
			log.debug("get successful");
			return list;
		} 
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<Item> findItemsByFields(String descripcion,String modelo) 
			throws RuntimeException{
		log.debug("getting all Items instances " );
		try {
			Query query;
			String q="SELECT c FROM Item c  WHERE "
					+  "c.descripcion LIKE :descripcion  "
					+  "AND c.modelo LIKE :modelo";
			
			
			query = entityManager.createQuery(q);	
				
			query.setParameter("descripcion","%" + descripcion + "%" );
			query.setParameter("modelo","%" + modelo + "%");
				
			List<Item> list = query.getResultList();
			log.debug("get successful");
			
			return list;
		} 
		catch (RuntimeException re) {
			log.error("get failed", re);
			return null;
			//throw re;
			
		}
	}
	
	public int updateItem(BigDecimal itemId, String descripcion, String modelo,
							BigDecimal costoVenta, BigDecimal costoAlquiler, String itemFoto,
							BigDecimal impuesto, BigDecimal descuento, Short numeroUnidades,
							BigDecimal costoHora, BigDecimal costoDiario) {
		
		int updatedCliente;
		log.debug("updating Item instance " );
		try {
			
			Query query;
			query = entityManager.createQuery("UPDATE Item c SET c.descripcion = :descripcion,"
																				+  "c.modelo = :modelo,"
																				+  "c.costoVenta = :costoVenta,"
																				+  "c.costoAlquiler = :costoAlquiler,"
																				+  "c.itemFoto = :itemFoto,"
																				+  "c.impuesto = :impuesto,"
																				+  "c.descuento = :descuento,"
																				+  "c.numeroUnidades = :numeroUnidades,"
																				+  "c.costoHora = :costoHora, "
																				+  "c.costoDiario = :costoDiario, "
																				+ " WHERE c.itemId = :itemId");
			
																				
			query.setParameter("itemId",itemId);
			query.setParameter("descripcion",descripcion);
			query.setParameter("modelo",modelo);
			query.setParameter("costoVenta",costoVenta);
			query.setParameter("costoAlquiler",costoAlquiler);
			query.setParameter("itemFoto",itemFoto);
			query.setParameter("impuesto",impuesto);																	
			query.setParameter("descuento",descuento);
			query.setParameter("numeroUnidades",numeroUnidades);
			query.setParameter("costoHora",costoHora);
			query.setParameter("costoDiario",costoDiario);
			
			updatedCliente = query.executeUpdate();
			return updatedCliente;
		}			
		catch (RuntimeException re) {
			log.error("get failed", re);				
			return -1;
			
		}
	}

	

	
}
